package com.jcfk.system.controller;

import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.security.annotation.AllowAnonymous;
import com.jcfk.common.web.domain.*;
import com.jcfk.system.api.utils.DictionaryUtils;
import com.jcfk.system.domain.dto.SysLogDTO;
import com.jcfk.system.domain.dto.SysSimpleLogDTO;
import com.jcfk.system.domain.dto.SysUserDTO;
import com.jcfk.system.domain.query.SysLogQuery;
import com.jcfk.system.domain.query.SysUserQuery;
import com.jcfk.system.service.SysOperateLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 操作日志 服务接口
 * @author lbh
 * @date 2023-11-16
 */
@Api(value = "/sysOperateLog", tags = {"操作日志 服务接口"})
@RestController
@RequestMapping("/sysOperateLog")
public class SysOperateLogController {

    /**
     * 操作日志服务
     */
    @Autowired
    private SysOperateLogService sysOperateLogService;

    /**
     * 字典工具
     */
    @Autowired
    private DictionaryUtils dictionaryUtils;

    /**
     * 查询操作日志信息列表(分页)
     * @param requestParam
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysLogQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation(value = "查询操作日志信息列表(分页)", notes = "查询操作日志信息列表(分页)", httpMethod = "POST")
    @PostMapping("queryByPage")
    public RetPage<SysLogDTO> queryByPage(@RequestBody ReqPageParam<SysLogQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<SysLogQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("requestTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }

        DataPage<SysLogDTO> page = new DataPage<SysLogDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                SysLogDTO.class);
        page = sysOperateLogService.queryByPage(page,requestParam.getData());

        //绑定日志级别文本
        dictionaryUtils.bindDictionaryLabel(page.getRecords(),"LogLevel","LogLevel","LogLevelName");

        return new RetPage<SysLogDTO>(page);
    }

    /**
     * 获取日志
     *
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "获取日志", notes = "获取日志", httpMethod = "POST")
    @PostMapping("/get/{id}")
    @ResponseBody
    public RetResult<SysLogDTO> get(@NonNull @PathVariable("id") String id) {
        SysLogDTO log = sysOperateLogService.get(id);
        return RetResult.ok(log);
    }

    /**
     * 插入日志
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "插入日志", notes = "插入日志", httpMethod = "POST")
    @PostMapping("insert")
    @ResponseBody
    @AllowAnonymous
    public RetResult<?> insert(@RequestBody SysSimpleLogDTO logDTO) {
        sysOperateLogService.insert(logDTO);
        return RetResult.ok();
    }

    public void getModularAccessSummary(){

    }
}
